package com.oneplatform.system.dao.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.jeesuite.mybatis.core.BaseMapper;
import com.oneplatform.system.dao.entity.SequenceRuleEntity;
import com.oneplatform.system.dto.param.SequenceRuleQueryParam;

public interface SequenceRuleEntityMapper extends BaseMapper<SequenceRuleEntity, Integer> {
	
	List<SequenceRuleEntity> findListByParam(SequenceRuleQueryParam param);
	
	
	@Select("SELECT * FROM sequence_rules WHERE app_id=#{appId} AND code=#{code} LIMIT 1")
	@ResultMap("BaseResultMap")
	SequenceRuleEntity findByAppIdAndCode(@Param("appId") String appId,@Param("code") String code);
	
	@Select("SELECT last_sequence FROM sequence_rules WHERE app_id=#{appId} AND code=#{code} LIMIT 1")
	@ResultType(Integer.class)
	Integer findLastSequence(@Param("appId") String appId,@Param("code") String code);
	
	@Update("UPDATE sequence_rules SET last_sequence=last_sequence+#{incr} WHERE app_id=#{appId} AND code=#{code} AND last_sequence=#{expectLastSequence}")
	@ResultType(Integer.class)
	int updateLastSequence(@Param("appId") String appId,@Param("code") String code,@Param("incr") int incr,@Param("expectLastSequence") int expectLastSequence);
}